import Loading from '../components/widget/Loading';
import {request} from '../utils/request';
import {action, flow, makeAutoObservable, observable} from 'mobx';
/**
 * home store 全局存储
 */
class ArticleDetailStore {
  detail: Article = {} as Article;
  constructor() {
    makeAutoObservable(this, {
      detail: observable,
      requestArticleDetail: action,
    });
  }

  requestArticleDetail = flow(function* (
    this: ArticleDetailStore, // 方便使用this指针
    id: number,
  ) {
    Loading.show();
    try {
      const params = {
        id,
      };

      const {data} = yield request('articleDetail', params);
      this.detail = data || {};
    } catch (error) {
      console.log(error);
    } finally {
      Loading.hide();
    }
  });
}

/**
 * ESM单例导出
 */
export default new ArticleDetailStore();
